Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize CPU usage #1725

Merged
merged 3 commits into from
Dec 10, 2019
Merged

Initialize CPU usage #1725

merged 3 commits into from
Dec 10, 2019

Conversation

narrieta
Copy link
Member

@narrieta narrieta commented Dec 4, 2019

The CPU usage for the cgroup was not initialized when creating the CpuCgroup (the system usage was).

Now initialization sets both values and is done when we start tracking the cgroup (as opposed as when it is created). We can create cgroups and never track them so this is a better place for initialization.

Now CPU is computed as a percentage of usage for all cores (e.g. if a cgroup was using 2 cores at 100% on a 4-core system, now we report 50% instead of 200%). This makes data easier to use.

Also, added field #8 of /proc/stat to the computation of CPU (see "man proc"):

steal (since Linux 2.6.11)
(8) Stolen time, which is the time spent in other operating systems when running in a virtualized environment


This change is Reviewable

@narrieta narrieta requested review from vrdmr, pgombar and larohra December 4, 2019 20:20
@narrieta narrieta added this to the v2.2.46 milestone Dec 4, 2019
@codecov
Copy link

codecov bot commented Dec 4, 2019

Codecov Report

Merging #1725 into develop will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1725      +/-   ##
===========================================
+ Coverage    68.16%   68.17%   +0.01%     
===========================================
  Files           80       80              
  Lines        11609    11617       +8     
  Branches      1631     1635       +4     
===========================================
+ Hits          7913     7920       +7     
  Misses        3359     3359              
- Partials       337      338       +1
Impacted Files Coverage Δ
azurelinuxagent/common/cgroup.py 91.86% <100%> (+1.19%) ⬆️
azurelinuxagent/common/osutil/default.py 59.79% <100%> (-0.24%) ⬇️
azurelinuxagent/common/cgroupstelemetry.py 96.64% <100%> (+0.06%) ⬆️
azurelinuxagent/daemon/resourcedisk/default.py 34.13% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 00c75b1...1b938a1. Read the comment docs.

def get_cpu_usage(self):
"""
Collects and return the cpu usage.
cgroup_delta = self._current_cgroup_cpu - self._previous_cgroup_cpu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the absolute value here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to my comment: I assume that since we care about the ratio of the deltas, and assuming that if the cgroup delta is negative, then the system one will be, they will cancel each other out.
I'm not sure this is always the case though, that the sign is always the same for both deltas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CPU usage is measured in ticks. More recent values would never be less than older values so the difference (both for cgroup and system) would always be positive (or zero).

pgombar
pgombar previously approved these changes Dec 5, 2019
vrdmr
vrdmr previously approved these changes Dec 6, 2019
Copy link
Member

@vrdmr vrdmr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

def _cpu_usage_initialized(self):
return self._current_cgroup_cpu is not None and self._current_system_cpu is not None

def initialize_cpu_usage(self):
Copy link
Member

@vrdmr vrdmr Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call initialize_cpu_usage within the __init__ itself? Asking for my knowledge.

Copy link
Member Author

@narrieta narrieta Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get*cgroup functions instantiate cgroups that may never be tracked... I thought it was not worth initializing the cpu usage for those (especially since the actual cgroups may not exist on disk yet --- if we initialize on init then we would need to ensure the cgroups exist before calling the getter).

@@ -1354,7 +1354,7 @@ def get_total_cpu_ticks_since_boot():
if proc_stat is not None:
for line in proc_stat.splitlines():
if ALL_CPUS_REGEX.match(line):
system_cpu = sum(int(i) for i in line.split()[1:7])
system_cpu = sum(int(i) for i in line.split()[1:8])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment on what the columns are?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I'll add a comment referencing what man page can be consulted (description can be sort of long to include here)

tests/common/test_cgroups.py Show resolved Hide resolved
@narrieta narrieta dismissed stale reviews from vrdmr and pgombar via edb3218 December 7, 2019 00:37
Copy link
Contributor

@pgombar pgombar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@narrieta narrieta merged commit cf3bce5 into Azure:develop Dec 10, 2019
@narrieta narrieta deleted the cpu_usage branch December 10, 2019 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants